package com.example.smssender;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.text.DecimalFormat;
import java.util.List;

/**
 * 发送记录列表适配器
 */
public class SmsSendRecordAdapter extends RecyclerView.Adapter<SmsSendRecordAdapter.ViewHolder> {
    
    private List<SmsSendRecord> records;
    private OnRecordClickListener listener;
    
    public interface OnRecordClickListener {
        void onRecordClick(SmsSendRecord record);
        void onRecordDelete(SmsSendRecord record);
    }
    
    public SmsSendRecordAdapter(List<SmsSendRecord> records, OnRecordClickListener listener) {
        this.records = records;
        this.listener = listener;
    }
    
    public void updateRecords(List<SmsSendRecord> newRecords) {
        this.records = newRecords;
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sms_send_record, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (records == null || position < 0 || position >= records.size()) {
            return;
        }
        SmsSendRecord record = records.get(position);
        if (record == null) {
            return;
        }
        
        // 时间
        holder.tvTime.setText(record.getFriendlyDateTime());
        
        // 统计信息 - 分别显示
        holder.tvSuccessCount.setText(String.valueOf(record.getSuccessCount()));
        holder.tvFailCount.setText(String.valueOf(record.getFailCount()));
        holder.tvTotalCount.setText(String.valueOf(record.getTotalContacts()));
        
        // 成功率
        DecimalFormat df = new DecimalFormat("#.##");
        double successRate = record.getSuccessRate();
        String successRateText = df.format(successRate) + "%";
        holder.tvSuccessRate.setText(successRateText);
        
        // 根据成功率设置徽章背景颜色和进度条颜色
        int badgeColor;
        int progressColor;
        if (successRate >= 100) {
            badgeColor = Color.parseColor("#4CAF50"); // 绿色
            progressColor = Color.parseColor("#4CAF50");
        } else if (successRate >= 50) {
            badgeColor = Color.parseColor("#FF9800"); // 橙色
            progressColor = Color.parseColor("#FF9800");
        } else {
            badgeColor = Color.parseColor("#F44336"); // 红色
            progressColor = Color.parseColor("#F44336");
        }
        
        // 设置徽章背景颜色
        android.graphics.drawable.GradientDrawable badgeBg = new android.graphics.drawable.GradientDrawable();
        badgeBg.setShape(android.graphics.drawable.GradientDrawable.RECTANGLE);
        badgeBg.setCornerRadius(12f);
        badgeBg.setColor(badgeColor);
        holder.tvSuccessRate.setBackground(badgeBg);
        
        // 设置进度条
        holder.progressBar.setProgress((int) successRate);
        holder.progressBar.getProgressDrawable().setColorFilter(progressColor, android.graphics.PorterDuff.Mode.SRC_IN);
        
        // 短信内容预览（只显示前50个字符）
        String content = record.getMessageContent();
        if (content != null && !content.isEmpty()) {
            String preview = content.length() > 50 ? content.substring(0, 50) + "..." : content;
            holder.tvContent.setText(preview);
            holder.llContentPreview.setVisibility(View.VISIBLE);
        } else {
            holder.llContentPreview.setVisibility(View.GONE);
        }
        
        // 点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRecordClick(record);
            }
        });
        
        // 删除按钮
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onRecordDelete(record);
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return records != null ? records.size() : 0;
    }
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTime;
        TextView tvSuccessCount;
        TextView tvFailCount;
        TextView tvTotalCount;
        TextView tvSuccessRate;
        ProgressBar progressBar;
        LinearLayout llContentPreview;
        TextView tvContent;
        TextView btnDelete;
        
        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvSuccessCount = itemView.findViewById(R.id.tvSuccessCount);
            tvFailCount = itemView.findViewById(R.id.tvFailCount);
            tvTotalCount = itemView.findViewById(R.id.tvTotalCount);
            tvSuccessRate = itemView.findViewById(R.id.tvSuccessRate);
            progressBar = itemView.findViewById(R.id.progressBar);
            llContentPreview = itemView.findViewById(R.id.llContentPreview);
            tvContent = itemView.findViewById(R.id.tvContent);
            btnDelete = itemView.findViewById(R.id.btnDelete);
        }
    }
}

